#include "TranSrv.h"

#include <QtSql>

#include "ComStruct.h"
#include "rtdb.h"
#include "ModbusFactory.h"
#include "DataTranSrv.h"
#include "AppIniFile.h"
#include "LogFile.h"
#include "AppDefine.h"

CTranSrv* g_pLogic = 0;

CTranSrv::CTranSrv(QObject *parent)
	: QObject(parent)
{
    Init();
}

void CTranSrv::Init()
{
    InitMembers();

    QObject::connect(m_pTimer, SIGNAL(timeout()), this, SLOT(saveRemoteSetParam()));
    printf("XnTranSrv Version %s RTDB %s\n", VERSION_TEXT, BAMS_VERSION_TEXT);
}

void CTranSrv::InitMembers()
{
    m_pModbusFactory = new CModbusFactory();//创建modbus工厂
    m_pDataTranSrv = new CDataTranSrv(this, m_pModbusFactory);//创建数据传输服务对象，关联父对象和工厂
    m_pTimer = new QTimer(this);//创建定时器，用于定时保存远程参数

    m_pTranslator = new QTranslator;//创建翻译器
    m_pIniFile = new CAppIniFile();//加载配置文件
    m_pIniFile->ReadFromIniFile();//配置文件系统
    m_pLogFile = new CLogFile();
    m_pLogFile->SetIsWriteLog(m_pIniFile->GetIsWriteLog());//是否写日志
    m_pLogFile->SetFileMaxSize(m_pIniFile->GetLogFileMaxSize());//设置最大日志文件大小
    m_pLogFile->SetFileNameType(m_pIniFile->GetLogFileNameType());//设置文件命名方式
    KG_LOG_INFO(m_pIniFile->GetRemark());//写一条备注日志记录
    g_pLogic = this;//设置全局指针，程序中访问主服务类的全局接口

    m_pRTDB = 0;
    m_pBattStack = 0;

	m_strDriver = "sqlite"; //DB Information默认数据库连接配置
	m_strIP = "127.0.0.1"; 
	m_usPort = 3306; 
	m_strDBName = "../db/bams.db"; 
	m_strUserName = "root"; 
	m_strPwd = "admin";

	QObject::connect(m_pTimer, SIGNAL(timeout()), this, SLOT(saveRemoteSetParam()));
	printf("XnTranSrv Version %d.%d RTDB %d.%d.%d\n", VERSION_MAJOR, VERSION_MINOR, BAMS_VERSION_MAJOR, BAMS_VERSION_MINOR, BAMS_VERSION_REVISION);
}

CTranSrv::~CTranSrv()
{
	if (m_pRTDB)
	{
		delete m_pRTDB;
		m_pRTDB = 0;
	}

	if (m_pDataTranSrv)
	{
		delete m_pDataTranSrv;
		m_pDataTranSrv = 0;
    }

	if (m_pModbusFactory)
	{
		delete m_pModbusFactory;
		m_pModbusFactory = 0;
	}

	if (m_pTimer)
	{
		delete m_pTimer;
		m_pTimer = 0;
	}

    if (m_pTranslator)
    {
        delete m_pTranslator;
        m_pTranslator = 0;
    }
	
    if (0 == m_pLogFile)
    {
        delete m_pLogFile;
        m_pLogFile = 0;
    }

    if (0 == m_pIniFile)
    {
        delete m_pIniFile;
        m_pIniFile = 0;
    }
}

int CTranSrv::init()
{
	// some variables
	int iRetVal = -999;
	int iTemp = 0;
	QString strTemp = "";

    m_pRTDB = new CRTDB();
    if (0 == m_pRTDB)
    {
        printf("create rtdb failed\n");
        KG_LOG_INFO(QString("分配RTDB内存时失败"));
        return -1;
    }

    if (!m_pRTDB->AttachRtdb())
    {
        printf("init rtdb failed\n");
        KG_LOG_INFO(QString("打开RTDB内存时失败"));
        return -2;
    }

    m_pBattStack = m_pRTDB->getStack(0);
    if (0 == m_pBattStack)
    {
        return -3;
    }

	//检查rtdb版本合程序是否兼容，防止不同版本数据结构误读
    if (BAMS_VERSION_MAJOR != m_pBattStack->uaRtdbVersMajor
        || BAMS_VERSION_MINOR != m_pBattStack->uaRtdbVersMinor
        || BAMS_VERSION_REVISION != m_pBattStack->uaRtdbVersRevision)
    {
        m_pBattStack->usVersIncomp = 104;
        return -4;
    }

    m_pBattStack->uaTranSrvVers = VERSION_MAJOR; //Version
    m_pBattStack->uaTranSrvVers = m_pBattStack->uaTranSrvVers << 4;
    m_pBattStack->uaTranSrvVers |= VERSION_MINOR;

    if (1 == m_pBattStack->usAlarmCacheStrategy) //告警缓存
    {
        m_pDataTranSrv->initAlarmCache();
    }

    if (openDB())
    {
            m_pDataTranSrv->setRTDB(m_pRTDB, m_pBattStack);
            m_pDataTranSrv->setPort(m_pIniFile->GetPort());
            m_pDataTranSrv->setCliIdleTime(m_pIniFile->GetIdleTime());
            m_pDataTranSrv->setModbusAddrOffset(m_pIniFile->GetModbusAddOffset());
            m_pDataTranSrv->SetProjectMode(m_pIniFile->GetProjectMode());
            m_pModbusFactory->SetProjectMode(m_pIniFile->GetProjectMode());
            m_pModbusFactory->SetRtdb(m_pRTDB);

        iRetVal= 0;
    }
    else
    {
        iRetVal = -3;
    }

    switchLang();

	m_pTimer->start(1000); // 1秒

	return iRetVal;
}

void CTranSrv::switchLang()
{
    if (1 == m_pBattStack->usLang) //语言        0 - 中文   1 - 英文 2-法文
    {
        m_pTranslator->load("../lang/xntransrv_en.qm");
        QCoreApplication::installTranslator(m_pTranslator);
    }
    else if (0 == m_pBattStack->usLang)
    {
        m_pTranslator->load("../lang/xntransrv_zh.qm");
        QCoreApplication::installTranslator(m_pTranslator);
    }
    else if (2 == m_pBattStack->usLang)
    {
        m_pTranslator->load("../lang/xntransrv_fr.qm");
        QCoreApplication::installTranslator(m_pTranslator);
    }
    else
    {
        m_pTranslator->load("../lang/xntransrv_zh.qm");
        QCoreApplication::installTranslator(m_pTranslator);
    }
}

bool CTranSrv::openDB()
{
	// read db.ini
	QSettings setDB("../ini/db.ini", QSettings::IniFormat);
	m_strDriver = setDB.value("DB/Driver", m_strDriver).toString().trimmed().toUpper();
	m_strIP = setDB.value("DB/IP", m_strIP).toString();
	m_usPort = (ushort)setDB.value("DB/Port", m_usPort).toUInt();
	m_strDBName = setDB.value("DB/DBName", m_strDBName).toString(); //SQLITE  DBName=../ini/xncfg.db
	m_strUserName = setDB.value("DB/UserName", m_strUserName).toString();
	m_strPwd = setDB.value("DB/Pwd", m_strPwd).toString();

	// Read info from DB
	QString strDBType = "QMYSQL";
	bool bDBOpen = false;

	m_strDriver = m_strDriver.trimmed().toUpper();

	if ("MYSQL" == m_strDriver)
	{
		strDBType = "QMYSQL";
	}
	else if ("ORACLE" == m_strDriver)
	{
		strDBType = "QOCI";
	}
	else if ("SQLITE" == m_strDriver)
	{
		strDBType = "QSQLITE";
	}

	QSqlDatabase db = QSqlDatabase::addDatabase(strDBType);
	db.setHostName(m_strIP);
	db.setPort(m_usPort);
	db.setDatabaseName(m_strDBName);
	db.setUserName(m_strUserName);
	db.setPassword(m_strPwd);

	if (db.open())
	{
		if ("QSQLITE" == strDBType)// 如是SQLITE数据库,需要判断表是否存在,来确定库是否打开 以工程信息表来判断 数据打开是否成功
		{
			QStringList strTableList = db.tables(QSql::Tables);

			if (!strTableList.contains("tblBattery", Qt::CaseInsensitive))//不存在该表
			{
				bDBOpen = false;
			}
			else
			{
				bDBOpen = true;
			}
		}
		else
		{
			bDBOpen = true;
		}
	}
	else
	{
		bDBOpen = false;
	}

	return bDBOpen;
}

void CTranSrv::startSrv()
{
	m_pDataTranSrv->startDataTranSrv();
    m_pDataTranSrv->start();
}

void CTranSrv::saveRemoteSetParam()
{
	QStringList lstSql;
	int iIndex = 0;
	CBattBank* pBankBank = 0;
	CBankAlarm* pBankAlarm = 0;
	
	// DelayBeforeCutCntor
    QString strText;
	if (m_pBattStack->usDlyBfCutCntorModifyCount > 0)
	{
		lstSql << QString("update tblSysParam set value = %1 where code = 'delayBeforeCutCntor';").arg(m_pBattStack->usDelayBeforeCutCntor);

        strText = tr("远程修改了 <接触器动作延时时间>");

        lstSql << QString(trUtf8("insert into tblLog (startDT, type, context) values ('%1', %2, '%3');"))
            .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(SYS_LOG_TYPE_RUN).arg(strText); // 远程修改了 <接触器动作延时时间>

		m_pBattStack->usDlyBfCutCntorModifyCount = 0;
	}

	// 接触器控制
	if (m_pBattStack->usEMSContModifyCount > 0)
	{
		if (1 == m_pBattStack->usEMSContCtrl) // 闭合
		{

            strText = tr("远程闭合 接触器.");

            lstSql << QString(("insert into tblLog (startDT, type, context) values ('%1', %2, '%3');"))
                .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(SYS_LOG_TYPE_RUN).arg(strText);

		}
		else if (2 == m_pBattStack->usEMSContCtrl) // 断开
		{

            strText = tr("远程断开 接触器.");

            lstSql << QString(("insert into tblLog (startDT, type, context) values ('%1', %2, '%3');"))
                .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(SYS_LOG_TYPE_RUN).arg(strText);

		}
		else
		{
            strText = tr("远程修改 接触器策略(BMS控制).");

            lstSql << QString(("insert into tblLog (startDT, type, context) values ('%1', %2, '%3');"))
                .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(SYS_LOG_TYPE_RUN).arg(strText);

		}

		m_pBattStack->usEMSContModifyCount = 0;
	}

	// 汇流开关控制
	if (m_pBattStack->usEMSConfModifyCount > 0)
	{
		if (1 == m_pBattStack->usEMSConfCtrl) // 闭合
		{

            strText = tr("远程闭合 汇流开关.");

            lstSql << QString(("insert into tblLog (startDT, type, context) values ('%1', %2, '%3');"))
                .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(SYS_LOG_TYPE_RUN).arg(strText);

		}
		else if (2 == m_pBattStack->usEMSConfCtrl) // 断开
		{

            strText = tr("远程断开 汇流开关.");

            lstSql << QString(("insert into tblLog (startDT, type, context) values ('%1', %2, '%3');"))
                .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(SYS_LOG_TYPE_RUN).arg(strText);

		}
		else
		{

            strText = tr("远程修改 汇流开关策略(BMS控制).");

            lstSql << QString(("insert into tblLog (startDT, type, context) values ('%1', %2, '%3');"))
                .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(SYS_LOG_TYPE_RUN).arg(strText);

		}

		m_pBattStack->usEMSConfModifyCount = 0;
	}

	// 故障清除
	if (1 == m_pBattStack->usEMSFaultClearModifyCount || 11 == m_pBattStack->usEMSFaultClearModifyCount)
	{

        strText = tr("远程清除故障.");

        lstSql << QString(("insert into tblLog (startDT, type, context) values ('%1', %2, '%3');"))
            .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(SYS_LOG_TYPE_RUN).arg(strText);


		m_pBattStack->usEMSFaultClearModifyCount = 2;
	}

	// 远程升级
	if (1 == m_pBattStack->usEMSNotifyUpgradeModifyCount || 11 == m_pBattStack->usEMSNotifyUpgradeModifyCount)
	{

        strText = tr("远程升级BMS.");
        lstSql << QString(("insert into tblLog (startDT, type, context) values ('%1', %2, '%3');"))
            .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(SYS_LOG_TYPE_RUN).arg(strText);


		m_pBattStack->usEMSNotifyUpgradeModifyCount = 2;
	}


	for (iIndex = 0; iIndex < m_pBattStack->usInstallBankNum; ++iIndex)
	{
		pBankAlarm = m_pRTDB->getBankAlarm(iIndex);
		pBankBank = m_pRTDB->getBank(iIndex);
		if (pBankAlarm && pBankBank)
		{
			// 预警参数
			if (1 == pBankBank->usWarnParamModifyCount || 11 == pBankBank->usWarnParamModifyCount)
			{
				lstSql << QString("update tblAlarmParam set bankVolUpLimit = %1, bankVolDownLimit = %2, chgCurUpLimit = %3, dchgCurUpLimit = %4, sglVolUpLimit = %5, sglVolDownLimit = %6, chgSglTempUpLimit = %7, "
					"chgSglTempDownLimit = %8, dchgSglTempUpLimit = %9, dchgSglTempDownLimit = %10, sglTempUpLimit = %11, sglTempDownLimit = %12, envTempUpLimit = %13, envTempDownLimit = %14, "
					"powerLineTempUpLimit = %15, bankSocUpLimit = %16, bankSocDownLimit = %17, posResDownLimit = %18, negResDownLimit = %19, packVolDiffUpLimit = %20, packTempDiffUpLimit = %21 where type = 10 and bankNo = %22;")
					.arg(pBankAlarm->param.fBankVolUpLimitWarn).arg(pBankAlarm->param.fBankVolDownLimitWarn).arg(pBankAlarm->param.fChargeCurUpLimitWarn)
					.arg(pBankAlarm->param.fDischargeCurUpLimitWarn).arg(pBankAlarm->param.fSglVolUpLimitWarn).arg(pBankAlarm->param.fSglVolDownLimitWarn)
					.arg(pBankAlarm->param.fChargeSglTempUpLimitWarn).arg(pBankAlarm->param.fChargeSglTempDownLimitWarn).arg(pBankAlarm->param.fDchargeSglTempUpLimitWarn)
					.arg(pBankAlarm->param.fDchargeSglTempDownLimitWarn).arg(pBankAlarm->param.fSglTempUpLimitWarn).arg(pBankAlarm->param.fSglTempDownLimitWarn)
					.arg(pBankAlarm->param.fEnvTempUpLimitWarn).arg(pBankAlarm->param.fEnvTempDownLimitWarn).arg(pBankAlarm->param.fPowerLineTempUpLimitWarn)
					.arg(pBankAlarm->param.fBankSocUpLimitWarn).arg(pBankAlarm->param.fBankSocDownLimitWarn).arg(pBankAlarm->param.fPosResDownLimitWarn)
					.arg(pBankAlarm->param.fNegResDownLimitWarn).arg(pBankAlarm->param.fPackVolDiffUpLimitWarn).arg(pBankAlarm->param.fPackTempDiffUpLimitWarn)
					.arg(iIndex + 1);


                strText = tr("远程修改了 <预警参数>.");
                lstSql << QString(trUtf8("insert into tblLog (startDT, type, context) values ('%1', %2, '%3');"))
                    .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(SYS_LOG_TYPE_RUN).arg(strText); // 远程修改了 <预警参数>


				pBankBank->usWarnParamModifyCount = 2;
			}

			// 告警参数
			if (1 == pBankBank->usAlarmParamModifyCount || 11 == pBankBank->usAlarmParamModifyCount)
			{

				lstSql <<  QString("update tblAlarmParam set bankVolUpLimit = %1, bankVolDownLimit = %2, chgCurUpLimit = %3, dchgCurUpLimit = %4, sglVolUpLimit = %5, sglVolDownLimit = %6, chgSglTempUpLimit = %7, "
					"chgSglTempDownLimit = %8, dchgSglTempUpLimit = %9, dchgSglTempDownLimit = %10, sglTempUpLimit = %11, sglTempDownLimit = %12, envTempUpLimit = %13, envTempDownLimit = %14, "
					"powerLineTempUpLimit = %15, bankSocUpLimit = %16, bankSocDownLimit = %17, posResDownLimit = %18, negResDownLimit = %19, packVolDiffUpLimit = %20, packTempDiffUpLimit = %21 where type = 20 and bankNo = %22;")
					.arg(pBankAlarm->param.fBankVolUpLimitAlarm).arg(pBankAlarm->param.fBankVolDownLimitAlarm).arg(pBankAlarm->param.fChargeCurUpLimitAlarm)
					.arg(pBankAlarm->param.fDischargeCurUpLimitAlarm).arg(pBankAlarm->param.fSglVolUpLimitAlarm).arg(pBankAlarm->param.fSglVolDownLimitAlarm)
					.arg(pBankAlarm->param.fChargeSglTempUpLimitAlarm).arg(pBankAlarm->param.fChargeSglTempDownLimitAlarm).arg(pBankAlarm->param.fDchargeSglTempUpLimitAlarm)
					.arg(pBankAlarm->param.fDchargeSglTempDownLimitAlarm).arg(pBankAlarm->param.fSglTempUpLimitAlarm).arg(pBankAlarm->param.fSglTempDownLimitAlarm)
					.arg(pBankAlarm->param.fEnvTempUpLimitAlarm).arg(pBankAlarm->param.fEnvTempDownLimitAlarm).arg(pBankAlarm->param.fPowerLineTempUpLimitAlarm)
					.arg(pBankAlarm->param.fBankSocUpLimitAlarm).arg(pBankAlarm->param.fBankSocDownLimitAlarm).arg(pBankAlarm->param.fPosResDownLimitAlarm)
					.arg(pBankAlarm->param.fNegResDownLimitAlarm).arg(pBankAlarm->param.fPackVolDiffUpLimitAlarm).arg(pBankAlarm->param.fPackTempDiffUpLimitAlarm)
					.arg(iIndex + 1);

                strText = tr("远程修改了 <告警参数>");

                lstSql << QString(trUtf8("insert into tblLog (startDT, type, context) values ('%1', %2, '%3');"))
                    .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(SYS_LOG_TYPE_RUN).arg(strText); // 远程修改了 <告警参数>


				pBankBank->usAlarmParamModifyCount = 2;
			}

			// 保护参数
			if (1 == pBankBank->usProtParamModifyCount || 11 == pBankBank->usProtParamModifyCount)
			{
				lstSql <<  QString("update tblAlarmParam set bankVolUpLimit = %1, bankVolDownLimit = %2, chgCurUpLimit = %3, dchgCurUpLimit = %4, sglVolUpLimit = %5, sglVolDownLimit = %6, chgSglTempUpLimit = %7, "
					"chgSglTempDownLimit = %8, dchgSglTempUpLimit = %9, dchgSglTempDownLimit = %10, sglTempUpLimit = %11, sglTempDownLimit = %12, envTempUpLimit = %13, envTempDownLimit = %14, "
					"powerLineTempUpLimit = %15, bankSocUpLimit = %16, bankSocDownLimit = %17, posResDownLimit = %18, negResDownLimit = %19, packVolDiffUpLimit = %20, packTempDiffUpLimit = %21 where type = 30 and bankNo = %22;")
					.arg(pBankAlarm->param.fBankVolUpLimitProt).arg(pBankAlarm->param.fBankVolDownLimitProt).arg(pBankAlarm->param.fChargeCurUpLimitProt)
					.arg(pBankAlarm->param.fDischargeCurUpLimitProt).arg(pBankAlarm->param.fSglVolUpLimitProt).arg(pBankAlarm->param.fSglVolDownLimitProt)
					.arg(pBankAlarm->param.fChargeSglTempUpLimitProt).arg(pBankAlarm->param.fChargeSglTempDownLimitProt).arg(pBankAlarm->param.fDchargeSglTempUpLimitProt)
					.arg(pBankAlarm->param.fDchargeSglTempDownLimitProt).arg(pBankAlarm->param.fSglTempUpLimitProt).arg(pBankAlarm->param.fSglTempDownLimitProt)
					.arg(pBankAlarm->param.fEnvTempUpLimitProt).arg(pBankAlarm->param.fEnvTempDownLimitProt).arg(pBankAlarm->param.fPowerLineTempUpLimitProt)
					.arg(pBankAlarm->param.fBankSocUpLimitProt).arg(pBankAlarm->param.fBankSocDownLimitProt).arg(pBankAlarm->param.fPosResDownLimitProt)
					.arg(pBankAlarm->param.fNegResDownLimitProt).arg(pBankAlarm->param.fPackVolDiffUpLimitProt).arg(pBankAlarm->param.fPackTempDiffUpLimitProt)
					.arg(iIndex + 1);


                strText = tr("远程修改了 <保护参数>");

                lstSql << QString(trUtf8("insert into tblLog (startDT, type, context) values ('%1', %2, '%3');"))
                    .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(SYS_LOG_TYPE_RUN).arg(strText); // 远程修改了 <保护参数>


				pBankBank->usProtParamModifyCount = 2;
			}

			// 预警回差
			if (1 == pBankBank->usWarnHCParamModifyCount || 11 == pBankBank->usWarnHCParamModifyCount)
			{
				lstSql << QString("update tblAlarmParam set bankVolUpLimit = %1, bankVolDownLimit = %2, chgCurUpLimit = %3, dchgCurUpLimit = %4, sglVolUpLimit = %5, sglVolDownLimit = %6, chgSglTempUpLimit = %7, "
					"chgSglTempDownLimit = %8, dchgSglTempUpLimit = %9, dchgSglTempDownLimit = %10, sglTempUpLimit = %11, sglTempDownLimit = %12, envTempUpLimit = %13, envTempDownLimit = %14, "
					"powerLineTempUpLimit = %15, bankSocUpLimit = %16, bankSocDownLimit = %17, posResDownLimit = %18, negResDownLimit = %19, packVolDiffUpLimit = %20, packTempDiffUpLimit = %21 where type = 11 and bankNo = %22;")
					.arg(pBankAlarm->param.fBankVolUpLimitWarnHC).arg(pBankAlarm->param.fBankVolDownLimitWarnHC).arg(pBankAlarm->param.fChargeCurUpLimitWarnHC)
					.arg(pBankAlarm->param.fDischargeCurUpLimitWarnHC).arg(pBankAlarm->param.fSglVolUpLimitWarnHC).arg(pBankAlarm->param.fSglVolDownLimitWarnHC)
					.arg(pBankAlarm->param.fChargeSglTempUpLimitWarnHC).arg(pBankAlarm->param.fChargeSglTempDownLimitWarnHC).arg(pBankAlarm->param.fDchargeSglTempUpLimitWarnHC)
					.arg(pBankAlarm->param.fDchargeSglTempDownLimitWarnHC).arg(pBankAlarm->param.fSglTempUpLimitWarnHC).arg(pBankAlarm->param.fSglTempDownLimitWarnHC)
					.arg(pBankAlarm->param.fEnvTempUpLimitWarnHC).arg(pBankAlarm->param.fEnvTempDownLimitWarnHC).arg(pBankAlarm->param.fPowerLineTempUpLimitWarnHC)
					.arg(pBankAlarm->param.fBankSocUpLimitWarnHC).arg(pBankAlarm->param.fBankSocDownLimitWarnHC).arg(pBankAlarm->param.fPosResDownLimitWarnHC)
					.arg(pBankAlarm->param.fNegResDownLimitWarnHC).arg(pBankAlarm->param.fPackVolDiffUpLimitWarnHC).arg(pBankAlarm->param.fPackTempDiffUpLimitWarnHC)
					.arg(iIndex + 1);

                strText = tr("远程修改了 <预警回差>");

                lstSql << QString(trUtf8("insert into tblLog (startDT, type, context) values ('%1', %2, '%3');"))
                    .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(SYS_LOG_TYPE_RUN).arg(strText); // 远程修改了 <预警回差>


				pBankBank->usWarnHCParamModifyCount = 2;
			}

			// 告警回差
			if (1 == pBankBank->usAlarmHCParamModifyCount || 11 == pBankBank->usAlarmHCParamModifyCount)
			{
				lstSql <<  QString("update tblAlarmParam set bankVolUpLimit = %1, bankVolDownLimit = %2, chgCurUpLimit = %3, dchgCurUpLimit = %4, sglVolUpLimit = %5, sglVolDownLimit = %6, chgSglTempUpLimit = %7, "
					"chgSglTempDownLimit = %8, dchgSglTempUpLimit = %9, dchgSglTempDownLimit = %10, sglTempUpLimit = %11, sglTempDownLimit = %12, envTempUpLimit = %13, envTempDownLimit = %14, "
					"powerLineTempUpLimit = %15, bankSocUpLimit = %16, bankSocDownLimit = %17, posResDownLimit = %18, negResDownLimit = %19, packVolDiffUpLimit = %20, packTempDiffUpLimit = %21 where type = 21 and bankNo = %22;")
					.arg(pBankAlarm->param.fBankVolUpLimitAlarmHC).arg(pBankAlarm->param.fBankVolDownLimitAlarmHC).arg(pBankAlarm->param.fChargeCurUpLimitAlarmHC)
					.arg(pBankAlarm->param.fDischargeCurUpLimitAlarmHC).arg(pBankAlarm->param.fSglVolUpLimitAlarmHC).arg(pBankAlarm->param.fSglVolDownLimitAlarmHC)
					.arg(pBankAlarm->param.fChargeSglTempUpLimitAlarmHC).arg(pBankAlarm->param.fChargeSglTempDownLimitAlarmHC).arg(pBankAlarm->param.fDchargeSglTempUpLimitAlarmHC)
					.arg(pBankAlarm->param.fDchargeSglTempDownLimitAlarmHC).arg(pBankAlarm->param.fSglTempUpLimitAlarmHC).arg(pBankAlarm->param.fSglTempDownLimitAlarmHC)
					.arg(pBankAlarm->param.fEnvTempUpLimitAlarmHC).arg(pBankAlarm->param.fEnvTempDownLimitAlarmHC).arg(pBankAlarm->param.fPowerLineTempUpLimitAlarmHC)
					.arg(pBankAlarm->param.fBankSocUpLimitAlarmHC).arg(pBankAlarm->param.fBankSocDownLimitAlarmHC).arg(pBankAlarm->param.fPosResDownLimitAlarmHC)
					.arg(pBankAlarm->param.fNegResDownLimitAlarmHC).arg(pBankAlarm->param.fPackVolDiffUpLimitAlarmHC).arg(pBankAlarm->param.fPackTempDiffUpLimitAlarmHC)
					.arg(iIndex + 1);


                strText = tr("远程修改了 <告警回差>");

                lstSql << QString(trUtf8("insert into tblLog (startDT, type, context) values ('%1', %2, '%3');"))
                    .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(SYS_LOG_TYPE_RUN).arg(strText); // 远程修改了 <告警回差>


				pBankBank->usAlarmHCParamModifyCount = 2;
			}

			// 保护回差
			if (1 == pBankBank->usProtHCParamModifyCount || 11 == pBankBank->usProtHCParamModifyCount)
			{
				lstSql <<  QString("update tblAlarmParam set bankVolUpLimit = %1, bankVolDownLimit = %2, chgCurUpLimit = %3, dchgCurUpLimit = %4, sglVolUpLimit = %5, sglVolDownLimit = %6, chgSglTempUpLimit = %7, "
					"chgSglTempDownLimit = %8, dchgSglTempUpLimit = %9, dchgSglTempDownLimit = %10, sglTempUpLimit = %11, sglTempDownLimit = %12, envTempUpLimit = %13, envTempDownLimit = %14, "
					"powerLineTempUpLimit = %15, bankSocUpLimit = %16, bankSocDownLimit = %17, posResDownLimit = %18, negResDownLimit = %19, packVolDiffUpLimit = %20, packTempDiffUpLimit = %21 where type = 31 and bankNo = %22;")
					.arg(pBankAlarm->param.fBankVolUpLimitProtHC).arg(pBankAlarm->param.fBankVolDownLimitProtHC).arg(pBankAlarm->param.fChargeCurUpLimitProtHC)
					.arg(pBankAlarm->param.fDischargeCurUpLimitProtHC).arg(pBankAlarm->param.fSglVolUpLimitProtHC).arg(pBankAlarm->param.fSglVolDownLimitProtHC)
					.arg(pBankAlarm->param.fChargeSglTempUpLimitProtHC).arg(pBankAlarm->param.fChargeSglTempDownLimitProtHC).arg(pBankAlarm->param.fDchargeSglTempUpLimitProtHC)
					.arg(pBankAlarm->param.fDchargeSglTempDownLimitProtHC).arg(pBankAlarm->param.fSglTempUpLimitProtHC).arg(pBankAlarm->param.fSglTempDownLimitProtHC)
					.arg(pBankAlarm->param.fEnvTempUpLimitProtHC).arg(pBankAlarm->param.fEnvTempDownLimitProtHC).arg(pBankAlarm->param.fPowerLineTempUpLimitProtHC)
					.arg(pBankAlarm->param.fBankSocUpLimitProtHC).arg(pBankAlarm->param.fBankSocDownLimitProtHC).arg(pBankAlarm->param.fPosResDownLimitProtHC)
					.arg(pBankAlarm->param.fNegResDownLimitProtHC).arg(pBankAlarm->param.fPackVolDiffUpLimitProtHC).arg(pBankAlarm->param.fPackTempDiffUpLimitProtHC)
					.arg(iIndex + 1);


                strText = tr("远程修改了 <保护回差>");

                lstSql << QString(trUtf8("insert into tblLog (startDT, type, context) values ('%1', %2, '%3');"))
                    .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(SYS_LOG_TYPE_RUN).arg(strText); // 远程修改了 <保护回差>


				pBankBank->usProtHCParamModifyCount = 2;
			}
		
			//// 监测数据			
			//if (pBankBank->usMonitorParamModifyCount > 0)
			//{
			//	lstSql << QString("update tblCrtCoe set bankVolCoe = %1, chargeCurCoe = %2, dischargeCurCoe = %3, envTempCoe = %4, sglVolCoe = %5, sglTempCoe = %6, bankSocRefUpLmtCoe = %7, bankSocRefDownLmtCoe = %8 where bankNo = %9;")
			//		.arg(pBankBank->fBankVolCoe).arg(pBankBank->fChargeCurCoe).arg(pBankBank->fDischargeCurCoe).arg(pBankBank->fEnvTempCoe)
			//		.arg(pBankBank->fSglVolCoe).arg(pBankBank->fSglTempCoe).arg(pBankBank->fBankSocRefUpLmtCoe).arg(pBankBank->fBankSocRefDownLmtCoe).arg(iIndex + 1);

			//	if ("en" == m_strLang)
			//	{
			//		lstSql << QString(("insert into tblLog (startDT, type, context) values ('%1', %2, 'Remote_Modify <Data_Adjust_Step>.');")) 
			//			.arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(SYS_LOG_TYPE_RUN); // 远程修改了 <监测数据调整步长>
			//	}
			//	else
			//	{
			//		lstSql << QString(trUtf8("insert into tblLog (startDT, type, context) values ('%1', %2, '\xE8\xBF\x9C\xE7\xA8\x8B\xE4\xBF\xAE\xE6\x94\xB9\xE4\xBA\x86\x20\x3C\xE7\x9B\x91\xE6\xB5\x8B\xE6\x95\xB0\xE6\x8D\xAE\xE8\xB0\x83\xE6\x95\xB4\xE6\xAD\xA5\xE9\x95\xBF\x3E.');")) 
			//			.arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(SYS_LOG_TYPE_RUN); // 远程修改了 <监测数据调整步长>
			//	}

			//	pBankBank->usMonitorParamModifyCount = 0;
			//}

			// EMS 总正接触器
			if (pBankBank->usEMSContModifyCountPos > 0)
			{
				if (1 == pBankBank->usEMSContCtrlPos) // 闭合
				{

                    strText = tr("远程闭合 组%1总正接触器.").arg(iIndex+1);

                    lstSql << QString(("insert into tblLog (startDT, type, context) values ('%1', %2, '%3');"))
                        .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(SYS_LOG_TYPE_RUN).arg(strText);

				}
				else if (2 == pBankBank->usEMSContCtrlPos) // 断开
				{

                    strText = tr("远程断开 组%1总正接触器.").arg(iIndex+1);

                    lstSql << QString(("insert into tblLog (startDT, type, context) values ('%1', %2, '%3');"))
                        .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(SYS_LOG_TYPE_RUN).arg(strText);

				}
				else
				{

                    strText = tr("远程修改 组%1总正接触器策略(BMS控制).").arg(iIndex+1);

                    lstSql << QString(("insert into tblLog (startDT, type, context) values ('%1', %2, '%3');"))
                        .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(SYS_LOG_TYPE_RUN).arg(strText);

				}

				pBankBank->usEMSContModifyCountPos = 0;
			}

			// EMS 总负接触器
			if (pBankBank->usEMSContModifyCountNeg > 0)
			{
				if (1 == pBankBank->usEMSContCtrlNeg) // 闭合
				{

                    strText = tr("远程闭合 组%1总负接触器").arg(iIndex+1);

                    lstSql << QString(("insert into tblLog (startDT, type, context) values ('%1', %2, '%3');"))
                        .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(SYS_LOG_TYPE_RUN).arg(strText);

				}
				else if (2 == pBankBank->usEMSContCtrlNeg) // 断开
				{

                    strText = tr("远程断开 组%1总负接触器.").arg(iIndex+1);

                    lstSql << QString(("insert into tblLog (startDT, type, context) values ('%1', %2, '%3');"))
                        .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(SYS_LOG_TYPE_RUN).arg(strText);

				}
				else
				{

                    strText = tr("远程修改 组%1总负接触器策略(BMS控制).").arg(iIndex+1);

                    lstSql << QString(("insert into tblLog (startDT, type, context) values ('%1', %2, '%3');"))
                        .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(SYS_LOG_TYPE_RUN).arg(strText);

				}

				pBankBank->usEMSContModifyCountNeg = 0;
			}
		}
	}
				
	if (lstSql.size() > 0)
	{
		QSqlDatabase db = QSqlDatabase::database();
		QSqlQuery query;

		db.transaction();
		for (iIndex = 0; iIndex < lstSql.size(); ++iIndex)
		{
			query.exec(lstSql.at(iIndex));
			query.clear();
		}
		db.commit(); 

		lstSql.clear();
		query.clear();
	}
}

void CTranSrv::CheckLogFile()
{
    if (0 == m_pLogFile) {
        m_pLogFile = new CLogFile();
        m_pLogFile->SetIsWriteLog(m_pIniFile->GetIsWriteLog());
        m_pLogFile->SetFileMaxSize(m_pIniFile->GetLogFileMaxSize());
        m_pLogFile->SetFileNameType(m_pIniFile->GetLogFileNameType());
    }
}

void CTranSrv::WriteLog(const QString& strLog)
{
    CheckLogFile();

    if (0 == m_pLogFile) return;

    // 获取日志文件的大小
    int fileSize = m_pLogFile->GetFileSize();

    // 如果文件大小超过最大值,则删除文件重新创建
    if (fileSize >= m_pIniFile->GetLogFileMaxSize()*1024*1024)
    {
        printf("log size:%d, too large to delete\n", fileSize);

        // 关闭日志文件
        m_pLogFile->CloseFile();
        // 获取日志文件名
        QString logFileName = m_pLogFile->GetFileName();
        // 创建文件
        QFile file(logFileName);
        // 删除文件
        file.remove();

        // 重新创建日志文件
        m_pLogFile->CreateFile(logFileName);
    }

    // 写入日志文件
    m_pLogFile->WriteLog(strLog);
}
